package io.renren.modules.shop.service.impl;

import cn.hutool.core.collection.IterUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.shop.entity.GoodsCoupon;
import io.renren.modules.shop.entity.UserAndCoupon;
import io.renren.modules.shop.mapper.GoodsCouponMapper;
import io.renren.modules.shop.mapper.UserAndCouponMapper;
import io.renren.modules.shop.service.GoodsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class GoodsCouponServiceImpl extends ServiceImpl<GoodsCouponMapper, GoodsCoupon> implements GoodsCouponService {

    @Autowired
    private UserAndCouponMapper userAndCouponMapper;
    @Override
    public int batchInsert(List<GoodsCoupon> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public List<GoodsCoupon> getUserCoupon(String openId) {
        UserAndCoupon userAndCoupon = new UserAndCoupon();
        userAndCoupon.setOpenid(openId);
        List<UserAndCoupon> list = userAndCouponMapper.selectList(new QueryWrapper<>(userAndCoupon));
        List<GoodsCoupon> goodsCoupons = this.baseMapper.selectList(null);
        Iterator<GoodsCoupon> iterator = goodsCoupons.iterator();
        if (list == null || list.size() == 0) {
            return goodsCoupons;
        }
        while (iterator.hasNext()) {
            for (UserAndCoupon andCoupon : list) {
                if (iterator.next().getCouponId().equals(andCoupon.getCouponId())) {
                    iterator.remove();
                    break;
                }
            }
        }
        return IterUtil.toList(iterator);
    }
}


